package cn.itcast.travel.service.impl;

import cn.itcast.travel.domain.collection;
import cn.itcast.travel.service.CollectionService;
import cn.itcast.travel.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.Collections;

public class CollectionServiceImpl implements CollectionService {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    //博客是否收藏
    @Override
    public boolean ifcollec(String bgid, String bloggerid) {
        collection c=null;
        try {
            String sql="select * from t_collection where bgid=? and bloggerid=?";
            c=template.queryForObject(sql,new BeanPropertyRowMapper<collection>(collection.class),bgid,bloggerid);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    //加入收藏或取消收藏
    @Override
    public void addcollec(String bgid, String bloggerid) {
        try{
            String sql="select * from t_collection where bgid=? and bloggerid=?";
            collection c=template.queryForObject(sql,new BeanPropertyRowMapper<collection>(collection.class),bgid,bloggerid);
            String sql1="delete from t_collection where bgid=? and bloggerid=?";
            template.update(sql1,bgid,bloggerid);
        }catch (Exception e){
            String sql="insert into t_collection(bgid,bloggerid) value(?,?)";
            template.update(sql,bgid,bloggerid);
        }
    }
}
